#include "Interpolation.hpp"
#include "Function.hpp"
#include "EquationSolver.hpp"
#include <iostream>
#include <vector>
#include <fstream>
class F1 : public Function {
    public:
        double operator() (double x) const{
            std::vector<std::vector<double>> data1 = {
                {0, 3, 5, 8, 13}, //time 
                {0, 225, 383, 623, 993}, //routine
                {75, 77, 80, 74, 72}  //speed
            };
            HermiteInterpolation interpolator1(data1);
            std::vector<double> routine = interpolator1.interpolate(x);
            return routine[0];
        }
    };
class F2 : public Function {
    public:
        double operator() (double x) const{
            F1 f1;
            return f1.derivative(x)-81;
        }
    };
int main(){
    F1 f1;
    
    std::cout << "Interpolated value at t = 10(Position): " << f1(10) << std::endl;
    
    std::cout << "Interpolated value at t = 10(Speed): " << f1.derivative(10) << std::endl;
    
    std::cout << "D:" << std::endl;
    Newton_Method solver_f2(F2(), 12);
    double x = solver_f2.solve();
    std::cout << "A root of the speed = 81 is: " << x << std::endl;

    std::ofstream file("data.csv");
    if (!file.is_open()) {
        std::cerr << "Failed to open file" << std::endl;
        return 1;
    }

    for (double x = 0; x <= 13; x += 0.01) {
        double speed = f1.derivative(x);
        file << x << "," << speed << std::endl;
    }
    file.close();
    
    return 0;
}